<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./lib/react.development.js"></script>
    <script src="./lib/react-dom.development.js"></script>
    <script src="./lib/babel.min.js"></script>
</head>

<body>
    <div id="root"></div>
    <script type="text/babel">
        // ref 可以在React当中获取到真实DOM
    // 应用场景：1-与组件结合使用 2-与普通元素结合使用
    class App extends React.Component {
        render(){
            return(
                <form name={'myForm'}>
                    <div ref={'myDiv'}>100</div>
                    <input ref={'userName'} name={"userName"} type="text"/>
                   <Child ref={"child"}></Child>
                    <button onClick={(e)=>{
                        e.preventDefault();
                         // 1
                       // console.log(document.querySelector("input").value)

                       // 2
                       // console.log(document.myForm.userName.value)

                       // 3- 使用ref
                        //   3-1: 为操作的React元素增加ref属性，值为字符串
                       //   3-2: 可以通过this.refs.字符串
                        // console.log(this.refs.userName === document.querySelector("input"));// true
                       // console.log(this.refs.userName.value);
                       // console.log(this.refs.myDiv.innerText);

                       // console.log(this.refs.child.state.a)
                       // console.log(this.refs.child.state.b)

                       this.refs.child.setState({
                           a:100,
                           b:200
                       })
                    }}>点我</button>
                </form>
            )
        }
    }

    class Child extends React.Component{
        state = {
            a:1,
            b:2
        }
        render(){
            return (
                <div>
                    <h3>Child</h3>
                    <p>a:{this.state.a}</p>
                    <p>b:{this.state.b}</p>
                </div>
            )
        }
    }
    ReactDOM.createRoot(document.getElementById('root')).render(<App/>)

    </script>
</body>

</html>